package com.ddtech.business.dance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.dance.entity.Dancer;
import com.ddtech.business.dance.entity.StandardDance;
import com.ddtech.business.dance.mapper.StandardDanceMapper;
import com.ddtech.business.dance.query.StandardDanceQuery;
import com.ddtech.business.dance.service.DancerService;
import com.ddtech.business.dance.service.StandardDanceService;
import com.ddtech.business.dance.vo.req.StandardDanceReqVo;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version v1.0
 * @ClassName:StandardDanceServiceImpl description：
 * @author:wei_zhang(xiao)
 * @date: 2024-07-16 13:25
 */
@Service
public class StandardDanceServiceImpl extends ServiceImpl<StandardDanceMapper, StandardDance> implements StandardDanceService {

    @Autowired
    private DancerService dancerService;

    @Resource
    private  StandardDanceMapper standardDanceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(StandardDanceReqVo standardDanceReqVo) {
        StandardDance standardDance = standardDanceReqVo.getStandardDance();
        standardDance.init();
        this.saveOrUpdate(standardDance);
        dancerService.saveDancer(standardDance,standardDanceReqVo.getDancerList());
        return  standardDance.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(String id) {
        this.removeById(id);
        if(StringUtils.isNotEmpty(id)){
            LambdaQueryWrapper<Dancer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dancer::getStandardDanceId, id);
            dancerService.remove(queryWrapper);
        }
    }

    @Override
    public StandardDance info(String id) {
        StandardDance standardDance = this.getById(id);
        standardDance.setDancerList(dancerService.queryByStandardDanceId(id));
//        List<Dancer> ls= dancerService.queryByStandardDanceId(id);
//        Map<String,Object> map =new HashMap<>();
//        map.put("standardDance",standardDance);
//        map.put("dancerList",ls);
        return standardDance;
    }

    @Override
    public Object pageList(StandardDanceQuery standardDanceQuery) {
        //IPage<StandardDance> page = this.page(standardDanceQuery.populatePage(), standardDanceQuery.autoWrapper());
        IPage<StandardDance> page =standardDanceMapper.pageStandardDanceList(standardDanceQuery.populatePage(), standardDanceQuery);
        List<StandardDance> list = page.getRecords();
        list.forEach(standardDance -> {
            standardDance.setDancerList(dancerService.queryByStandardDanceId( standardDance.getId()));
        });
       return PageUtil.toPage(list, page.getTotal());
    }
}
